package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaTable;
import com.wudl.govern.service.MetaTableService;
import com.wudl.govern.vo.MetaTableVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * meta_table Service
 *
 * @author wudl
 */
@Api(value = "元数据表管理")
@RestController
@RequestMapping("/metaTable")
public class MetaTableController {

    @Autowired
    private MetaTableService metaTableService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaTableVO>> page(@RequestBody MetaTableVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaTableVO> MetaTableVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaTable> MetaTableList = metaTableService.page(page, size);

        for (MetaTable metaTableTmp:MetaTableList )
        {
            MetaTableVO metaTableVO = new MetaTableVO();
            BeanUtil.copyProperties(metaTableTmp, MetaTableVO.class);
            MetaTableVoList.add(metaTableVO);
        }
        return CommonResult.success(MetaTableVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaTableVO> get(@PathVariable Object id) {
        MetaTable entity = metaTableService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaTableVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaTableVO addVO) {
        metaTableService.save(BeanUtil.copyProperties(addVO, MetaTable.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaTableVO editVO) {
        metaTableService.updateById(BeanUtil.copyProperties(editVO, MetaTable.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaTableService.removeById(id);
        return CommonResult.success();
    }

}
